﻿//==============================================================================
// File: WindowBehavior.cs
// Created: 2010-05-28
// Author: Piotr Włodek
//==============================================================================
// This file is a part of MAMMOTH project.
// Copyright (C) 2010 AGH University of Science and Technology, Krakow.
// https://caribou.iisg.agh.edu.pl/trac/mammoth
//
// Mammoth is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// 
// Mammoth is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Lesser General Public License for more details.
// 
// You should have received a copy of the GNU Lesser General Public License
// along with Mammoth. If not, see http://www.gnu.org/licenses/.
//==============================================================================

using System.ComponentModel;
using System.Windows;
using System.Windows.Threading;

namespace WpfRcp.Controls.Behaviors
{
    public static class WindowBehavior
    {
        #region HideInsteadClose Attached Property

        public static bool GetHideInsteadClose(DependencyObject obj)
        {
            return (bool)obj.GetValue(HideInsteadCloseProperty);
        }

        public static void SetHideInsteadClose(DependencyObject obj, bool value)
        {
            obj.SetValue(HideInsteadCloseProperty, value);
        }

        public static readonly DependencyProperty HideInsteadCloseProperty =
            DependencyProperty.RegisterAttached("HideInsteadClose", typeof(bool), typeof(WindowBehavior),
                new FrameworkPropertyMetadata(new PropertyChangedCallback(OnHideInsteadClose)));


        private static void OnHideInsteadClose(DependencyObject dependencyObject, DependencyPropertyChangedEventArgs e)
        {
            var window = dependencyObject as Window;
            if (window != null)
            {
                if ((bool)e.NewValue)
                {
                    // Handle closing event to hide window instead of closing it
                    window.Closing += delegate(object sender, CancelEventArgs args)
                    {
                        args.Cancel = true;

                        window.Dispatcher.BeginInvoke(DispatcherPriority.Background, (DispatcherOperationCallback)delegate
                        {
                            var cancelArgs = new CancelRoutedEventArgs(HidingEvent, window);
                            window.RaiseEvent(cancelArgs);

                            if (!cancelArgs.Cancel)
                            {
                                window.Hide();
                                window.RaiseEvent(new RoutedEventArgs(HiddenEvent, window)); 
                            }

                            return null;
                            }, null);
                        };
                    }
            }
        }

        #endregion

        #region Hidden Attached Event

        public static readonly RoutedEvent HiddenEvent = EventManager.RegisterRoutedEvent
            ("Hidden", RoutingStrategy.Bubble, typeof(RoutedEventHandler), typeof(WindowBehavior));

        public static void AddHiddenHandler(DependencyObject d, RoutedEventHandler handler)
        {
            var uie = d as UIElement;
            if (uie != null)
            {
                uie.AddHandler(HiddenEvent, handler);
            }
        }

        public static void RemoveHiddenHandler(DependencyObject d, RoutedEventHandler handler)
        {
            var uie = d as UIElement;
            if (uie != null)
            {
                uie.RemoveHandler(HiddenEvent, handler);
            }
        }

        #endregion

        #region Hiding Attached Event

        public static readonly RoutedEvent HidingEvent = EventManager.RegisterRoutedEvent(
            "Hiding", RoutingStrategy.Bubble, typeof(CancelRoutedEventHandler), typeof(WindowBehavior));

        public static void AddHidingHandler(DependencyObject d, CancelRoutedEventHandler handler)
        {
            var uie = d as UIElement;
            if (uie != null)
            {
                uie.AddHandler(HidingEvent, handler);
            }
        }

        public static void RemoveHidingHandler(DependencyObject d, CancelRoutedEventHandler handler)
        {
            var uie = d as UIElement;
            if (uie != null)
            {
                uie.RemoveHandler(HidingEvent, handler);
            }
        }

        #endregion
    }

    public delegate void CancelRoutedEventHandler(object sender, CancelRoutedEventArgs e);

    public sealed class CancelRoutedEventArgs : RoutedEventArgs
    {
        public CancelRoutedEventArgs()
        {
        }

        public CancelRoutedEventArgs(RoutedEvent routedEvent) : base(routedEvent)
        {
        }

        public CancelRoutedEventArgs(RoutedEvent routedEvent, object source) : base(routedEvent, source)
        {
        }

        public bool Cancel { get; set; }
    }
}